﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Collections;
using System.Collections.Specialized;
using ClientEndpoint;
using Package;
using System.Threading; 



namespace ClientATM
{
    public partial class ClientATM : Form
    {
        private List<string> config = new List<string>();
        private string nazwa = "";
         ArrayList ports = new ArrayList();
       // private int portTCPCloudReceive = 0;
         
     
        private int portTCPAgentSend = 0;
        private IPAddress CloudAddress =IPAddress.Parse("127.0.0.1");
        private int portTCPCloud = 50001;
        private int portTCPAgentListen = 0;
        private string[] args = Environment.GetCommandLineArgs();
        private System.Windows.Forms.Timer SendTimer = new System.Windows.Forms.Timer();
       
      
       
        private bool KeepAliveMessage=true;

        private ClientAgent agent = null;
        private System.Windows.Forms.Timer ka = null;

        private void cellhandler(Package.CellPackage package)
        {
            var cell = package.data as ATMCell.AtmCell;
            string odebrane="";
            listBox2.Items.Add("Odebrano: ");
            odebrane = System.Text.Encoding.UTF8.GetString(cell.data); 
            listBox2.Items.Add(odebrane);
            listBox2.Items.Add("=====EndOfCell=====");
        }

        void SendMessageTemporary()
        {
            
            SendTimer.Tick += new EventHandler(SendOnTick);
            SendTimer.Interval = 5000;
            SendTimer.Start();
            
        }
        void SendOnTick(object sender, EventArgs e)
        {
            try
            {
                var sender1 = Sender.getInstance();
                var port = Int32.Parse(textBox4.Text);
                var vpi = Int32.Parse(textBox2.Text);
                var vci = Int32.Parse(textBox3.Text);
                var message = textBox1.Text;
                if (ports.Contains(port) && textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
                {
                    sender1.sendMessage(message, port, vpi, vci);
                }
            }
            catch
            {
                MessageBox.Show("Podałeś błędne wartosci", "Blad", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        public ClientATM()
            {
            InitializeComponent();
            }

        private void InitializeAgent(string name, int listenPort, int sendPort)
            {
            agent = new ClientAgent(name, listenPort, sendPort, this);
            ka = new System.Windows.Forms.Timer();
            ka.Tick += new EventHandler(AmAlive);
            ka.Interval = 5000;
            ka.Start();
            }

        private void AmAlive(object sender, EventArgs e)
        {
            if (KeepAliveMessage == true)
            {
                agent.AmAlive();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ClientProperties.getInstance(nazwa, ports, HelloPackage.NodeType.UNI);
                var cCend = CableCloudEndPoint.getInstance(CloudAddress, portTCPCloud, cellhandler);
                int liczba = cCend.mainPort;
                listBox1.Items.Add(liczba);
                var receiving = new Thread(cCend.startListeningForCells) { IsBackground = true };
                receiving.Start();
                button1.Enabled = false;

            }
            catch
            {
                MessageBox.Show("Chmura nie działa!!!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            string Konfiguracja = "";


            StreamReader objReader = new StreamReader(args[1]);
            while (Konfiguracja != null)
            {
                Konfiguracja = objReader.ReadLine();
                if (Konfiguracja != null)
                    config.Add(Konfiguracja);

            }
            objReader.Close();
           
           
            nazwa = config[0];
       
           // portTCPCloudReceive = Int32.Parse(config[1]);
            portTCPAgentListen = Int32.Parse(config[1]);
            portTCPAgentSend = Int32.Parse(config[2]);
            InitializeAgent(nazwa, portTCPAgentListen, portTCPAgentSend);
           
           for(int i = 3; i<=5; i++)
            {
                
                ports.Add(Int32.Parse(config[i]));
                
            }
           button3.Enabled = false;
          
           button1.Enabled = true;
           for (int i = 0; i <= 5; i++)
           {
               listBox2.Items.Add("###Konfiguracja###" + config[i]);
           }    
        }
         
               
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {

                var sender1 = Sender.getInstance();
                var port = Int32.Parse(textBox4.Text);

                var vpi = Int32.Parse(textBox2.Text);

                var vci = Int32.Parse(textBox3.Text);

                var message = textBox1.Text;
                if (ports.Contains(port) && textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
                {
                    sender1.sendMessage(message, port, vpi, vci);
                }
                listBox1.Items.Add(message);
                
            }
            catch
            {
                MessageBox.Show("Podałeś błędne wartosci","Blad",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }


        }

        //wypisywanie do konsoli chmury
        public void Print(string info)
        {
            listBox2.Items.Add(info);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            SendMessageTemporary();
            button5.Enabled = false;
            button6.Enabled = true;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            SendTimer.Stop();
            SendTimer.Tick -= new EventHandler(SendOnTick);
            button5.Enabled = true;
            button6.Enabled = false;
        }

          
    }
}
        
   
